package frontend.container.taskPanel;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import frontend.MainFrame;
import frontend.base.BasicPanel;
import frontend.bean.ItemBean;

/**
 * 
 * the left panel of mainFrame<br>
 * this panel can refresh from the data
 * 
 * @author zhang.yingchun
 * 
 */
public class LeftPanel extends JPanel {

	private static final long serialVersionUID = -3334853339306869406L;

	private MainFrame parent;

	private BasicPanel leftTopPanel = new BasicPanel();
	private BasicPanel leftDownPanel = new BasicPanel();
	private JTaskXpanel taskPanel = null;;
	private JLabel leftTopPanelLbl = new JLabel("菜单管理");

	public LeftPanel(MainFrame parent) {
		this.parent = parent;
		initLeftPanel();
	}

	private void initLeftTopPanel() {
		leftTopPanelLbl.setForeground(new Color(38, 78, 99));
		leftTopPanelLbl.setFont(new Font("Dialog", Font.BOLD, 16));
		leftTopPanel.setLayout(new GridBagLayout());
		leftTopPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
//		gbc.insets = new Insets(5, 5, 5, 5);
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		leftTopPanel.add(leftTopPanelLbl, gbc);
	}

	private List<ItemBean> itemList;
	private GridBagConstraints gbc = new GridBagConstraints();

	private void initLeftDownPanel() {
		leftDownPanel.setLayout(new GridBagLayout());
	}

	private void init() {
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(leftTopPanel, gbc);
		
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(leftDownPanel, gbc);
		BasicPanel f = new BasicPanel();
		gbc.fill = GridBagConstraints.BOTH;
		JLabel fillLbl = new JLabel("s");
		gbc.weighty = 1;
		gbc.gridy = 2;
		f.add(fillLbl);
		add(f, gbc);
	}

	private void initLeftPanel() {
		initLeftTopPanel();
		initLeftDownPanel();
		init();
		setBackground(leftDownPanel.getBackground());
	}

	public void setListItem(List<ItemBean> list) {
		itemList = list;
		updateItem();
	}

	private void updateItem() {
		leftDownPanel.removeAll();
		createTaskPaneContainer(itemList);
		leftDownPanel.repaint();
		leftDownPanel.updateUI();
	}

	public void createTaskPaneContainer(List<ItemBean> list) {
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).isTitle()) {
				taskPanel = new JTaskXpanel();
				taskPanel.setFont(new Font("Dialog", Font.BOLD, 14));
				taskPanel.setTitleName(list.get(i).getText());
				gbc.gridx = 0;
				gbc.gridy = i;
				gbc.weightx = 1;
				gbc.anchor = GridBagConstraints.NORTHWEST;
				gbc.fill = GridBagConstraints.HORIZONTAL;
				leftDownPanel.add(taskPanel, gbc);
				continue;
			}
			TaskLabel taskLbl = new TaskLabel(list.get(i).getText());
			taskLbl.setToolTipText(list.get(i).getToolTip());
			taskLbl.setClassName(list.get(i).getClassName());
			taskLbl.setTaskId(list.get(i).getId());
			taskLbl.setParentClass(parent);
			taskPanel.setTask(taskLbl);
		}
	}
}
